package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.banquet.CrossPartyMgr;
import com.yanqu.road.server.manger.CrossMarriageGroupMgr;
import com.yanqu.road.server.pb.banquet.CrossBanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Cmd(code = CrossProtocol.C_CROSS_PARTY_LIST, desc = "")
public class CrossGetPartyListCmd extends CrossCommand {
    private long userId;
    private int type;
    private Set<Long> relativeList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        userId = packet.getPlayerId();
        BanquetProto.CrossBanquetListReqMsg req = BanquetProto.CrossBanquetListReqMsg.parseFrom(packet.getBytes());
        type = req.getType();
        relativeList = new HashSet<>(req.getRelativeListList());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        Map<String, CrossBanquetData> partyList = CrossPartyMgr.getPartyList(serverId, userId, type,relativeList, CrossMarriageGroupMgr.getPartyServerList(serverId));
        BanquetProto.BanquetListResp.Builder respMsg = BanquetProto.BanquetListResp.newBuilder();
        for (CrossBanquetData party : partyList.values()) {
            int pos = CrossPartyMgr.getUserPost(party.getBanquetId(), userId);
            BanquetProto.BanquetTemp.Builder tempMsg = CrossBanquetPb.parseBanquetDataTempMsg(party, pos > 0, relativeList.contains(party.getUserId()),
                    party.getServerId() != serverId);
            respMsg.addBanquetDataList(tempMsg);
        }
        respMsg.setRet(ret);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_BANQUET_LIST, respMsg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
